﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Maticsoft.DBUtility;
using System.Data.SqlClient;
using System.Data;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 业务逻辑类Rc 的摘要说明。
    /// </summary>
    public class Rc
    {
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="name">字段名字</param>
        /// <returns>得到一个对象实体</returns>
        public ElingOA6_Admin_DSJCJ.Model.Rc GetModel(string name)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 name,value,memo from [nc].nc_rc ");
            strSql.Append(" where name=@name");
            SqlParameter[] parameters = {
					new SqlParameter("@name", SqlDbType.VarChar,255)};
            parameters[0].Value = name;

            ElingOA6_Admin_DSJCJ.Model.Rc model = new ElingOA6_Admin_DSJCJ.Model.Rc();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="name">字段名字</param>
        /// <returns>是否存在</returns>
        public bool Exists(string name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [nc].nc_rc");
            strSql.Append(" where name=@name ");
            SqlParameter[] parameters = {
					new SqlParameter("@name", SqlDbType.VarChar,255)};
            parameters[0].Value = name;


            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model">model对象</param>
        /// <returns>数据增加</returns>
        public bool Add(ElingOA6_Admin_DSJCJ.Model.Rc model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [nc].nc_rc(");
            strSql.Append("name,value,memo)");
            strSql.Append(" values (");
            strSql.Append("@name,@value,@memo)");
            SqlParameter[] parameters = {
					new SqlParameter("@name", SqlDbType.VarChar,255),
					new SqlParameter("@value", SqlDbType.VarChar,255),
					new SqlParameter("@memo", SqlDbType.VarChar,255)};
            parameters[0].Value = model.name;
            parameters[1].Value = model.value;
            parameters[2].Value = model.memo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="model">model对象</param>
        /// <returns>是否更新</returns>
        public bool Update(ElingOA6_Admin_DSJCJ.Model.Rc model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [nc].nc_rc set ");
            strSql.Append("name=@name,");
            strSql.Append("value=@value");
            strSql.Append(" where name=@name ");
            SqlParameter[] parameters = {
					new SqlParameter("@name", SqlDbType.VarChar,255),
                    new SqlParameter("@value", SqlDbType.VarChar,255)};
            parameters[0].Value = model.name;
            parameters[1].Value = model.value;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="row">行</param>
        /// <returns>model对象</returns>
        public ElingOA6_Admin_DSJCJ.Model.Rc DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.Rc model = new ElingOA6_Admin_DSJCJ.Model.Rc();
            if (row != null)
            {
                if (row["name"] != null)
                {
                    model.name = row["name"].ToString();
                }
                if (row["value"] != null)
                {
                    model.value = row["value"].ToString();
                }
                if (row["memo"] != null)
                {
                    model.memo = row["memo"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 数据恢复
        ///</summary>
        ///<param name="str">sql语句</param>
        ///<param name="times">超时时间</param>
        public void Recover(string str, int times)
        {
            DbHelperSQL.ExecuteSqlByTime(str, times);

        }
    }
}
